<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of SMSController
 *
 * @author jack
 */
class SMSController extends ApiController {

    /**
     * @ApiDoc(
     *      description="短信发送"
     * )
     * @RequestParam(name="mobile", requirements="135xxxxxxxx,135xxxxxxxx", description="手机号码")
     * @RequestParam(name="content", requirements="string", description="内容")
     * @RequestParam(name="api", requirements="integer", description="1亿美(默认)2维拓")
     */
    public function sendAction() {
        $request = $this->getRequest();

        if ($request->request->has('api')) {
            $api = $request->request->get('api') == '2' ? 2 : 1;
        } else {
            $api = 1;
        }

        $view = $this->getFOSView();

        if ($request->request->has('mobile') && $request->request->has('content')) {
            $status = array();
            /* @var $sms \LHWApp_SMS */
            if ($api == 1) {
                $sms = $this->get('sms.service_ym');
            } else {
                $sms = $this->get('sms.service_wt');
            }
            $mobile = $request->request->get('mobile');
            $content = $request->request->get('content');
            $sms->sendSMS($mobile, $content);

            if ($api == 2) {
                // 記錄發送
                $em = $this->getEntityManager();
                $messageLog = new \Wilead\AdminBundle\Entity\MessageLog();
                $messageLog->setMobile($mobile);
                $messageLog->setContent($content);
                $em->persist($messageLog);
                $em->flush();
            }

            $status['send_sms'] = '成功';
            $view->setData($status);
            return $view;
        }
        $view->setStatusCode(403);
        return $view;
    }

}

?>
